package xyz.ljcc.ccmt.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import xyz.ljcc.ccmt.commons.utils.image.GraphicHelper;
import xyz.ljcc.ccmt.model.base.Dept;
import xyz.ljcc.ccmt.model.base.Operation;
import xyz.ljcc.ccmt.model.base.User;
import xyz.ljcc.ccmt.model.result.ResultModel;
import xyz.ljcc.ccmt.model.vo.PatientMasterInfoVo;
import xyz.ljcc.ccmt.service.api.admin.DeptService;
import xyz.ljcc.ccmt.service.api.admin.OperationService;
import xyz.ljcc.ccmt.service.api.admin.PatMasterInforService;
import xyz.ljcc.ccmt.service.api.admin.RoleService;
import xyz.ljcc.ccmt.service.api.admin.UserService;

@RestController
public class LoginHandler {
	
	@Autowired
	UserService userService;
	
	@Autowired
	DeptService deptService;
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	OperationService operationService;
	
	@Autowired
	PatMasterInforService patMasterInforService;
	
	@PostMapping("/doLogin")
	public ResultModel doLogin(String loginName,String password,HttpSession session) {
//		System.out.println("1111111111111111111111111111");
		ResultModel result = new ResultModel();
		User user = userService.getUser(loginName, password); 
		if(user == null) {
			result.setSuccess(false);
			result.setMessage("用户名或密码错误！");
		}else {
			session.setAttribute("user", user);
			result.setSuccess(true);
			result.setMessage("登录成功！");
			result.setData(user);
		}
		return result;
	}
	
	/**
	 * 生成验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@GetMapping(value="/verify/regist")
	public void verifyRegist(HttpSession session,HttpServletResponse response) throws IOException {
		final int width = 120; // 图片宽度  
	    final int height = 35; // 图片高度  
	    final String imgType = "jpeg"; // 指定图片格式 (不是指MIME类型)  
	    final OutputStream output = response.getOutputStream(); // 获得可以向客户端返回图片的输出流  
	    String code = GraphicHelper.create(width, height, imgType, output);  
	    session.setAttribute("verify/regist", code);
	}
	
	/**
	 * 验证码校验
	 * @param session
	 * @param code
	 * @return
	 */
	@PostMapping(value="/verify/check")
	public ResultModel verifyCheck(HttpSession session,String code){
		String codeBefore = (String) session.getAttribute("verify/regist");
//		System.out.println(codeBefore);
		session.removeAttribute("verify/regist"); // 移除验证码
		if(codeBefore.toUpperCase().equals(code.toUpperCase())) {
			return new ResultModel(true, "验证成功");
		}else {
			return new ResultModel(false, "验证码不匹配");
		}
	}
	
	/**
	 * 登陆者权限
	 * @param loginName
	 * @return
	 */
	@PostMapping(value="/loadUserAuthority")
	public ResultModel loadUserAuthority(String loginName) {
		ResultModel result = new ResultModel();
		// 获取科室权限信息
		List<Dept> depts = deptService.getDeptsByloginName(loginName); 
		// 获取用户角色信息
		String roles = roleService.getRolesByLoginName(loginName);
		// 获取用户菜单信息
		List<Operation> operations =  operationService.loadOperationsByLoginName(loginName);
		
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("LOCAL_DEPTS", depts);
		dataMap.put("LOCAL_ROELS", roles);
		dataMap.put("operations", operations);
		result.setData(dataMap);
		result.setSuccess(true);
		result.setMessage("查询成功");
		return result;
	}
	
	/**
	 * 获取当前科室患者列表
	 * @param deptCode
	 * @return
	 */
	@PostMapping(value="/loadPatsList")
	public ResultModel loadPatsList(String deptCode) {
		List<PatientMasterInfoVo> patientInfoVos = patMasterInforService.getPatientList(deptCode);
		return new ResultModel(true, patientInfoVos, "查询成功");
	}
	
	@GetMapping("/getpass/{password}")
	public String getPassword(@PathVariable("password") String password) {
		return userService.getPassword(password);
	}
	
	
	
	
}
